package com.action.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.action.common.exception.BizCodeEnum;
import com.action.common.to.SkuStockTo;
import com.action.gulimall.ware.vo.SkuHasStockVo;
import com.action.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.action.gulimall.ware.entity.WareSkuEntity;
import com.action.gulimall.ware.service.WareSkuService;
import com.action.common.utils.PageUtils;
import com.action.common.utils.R;



/**
 * 商品库存
 *
 * @author shilun
 * @email shilun217@qq.com
 * @date 2021-06-09 23:17:11
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /** 锁定订单所有订单项的库存 **/
    @PostMapping(value = "/orderLockStock")
    public R orderLockStock(@RequestBody WareSkuLockVo lockVo){
        Boolean lockSuccess=wareSkuService.orderLockStock(lockVo);
        if(lockSuccess){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.ORDER_LOCK_STOCK_EXCEPTION.getCode(),BizCodeEnum.ORDER_LOCK_STOCK_EXCEPTION.getMessage());
        }

    }

    /**
     * 查询sku是否有库存
     * @return
     */
    @PostMapping(value = "/hasStock")
    public R getSkuHasStock(@RequestBody List<Long> skuIds) {

        //skuId stock
        List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);

        return R.ok().setData(vos);

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/ware/waresku/skuIds/hasstock")
    public R skusHasStocks(@RequestBody List<Long> skuIds){
        List<SkuStockTo> skuStockTos = wareSkuService.skusHasStocks(skuIds);
        return R.ok().setData(skuStockTos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
